/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    FrmSelectWorksheet.cs

Abstract: 

    Form to select Excel Worksheets to retrieve their columns data   
     
--*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UtilityManager;
using ExcelManager;
using System.Globalization;
using AddIn.Windows.Forms;
using System.Reflection;

namespace RackServerManager
{
    public partial class FrmSelectWorksheet : AddIn.Windows.Forms.InteriorWizardPage
    {
        #region Form Variables
        private System.Windows.Forms.ComboBox cmbSelectColo;  
        private System.Windows.Forms.Label lblSelectColo;
        private System.Windows.Forms.Label titleLabel;
        private System.Windows.Forms.ComboBox cmbSelectRack;      
        private System.Windows.Forms.ComboBox cmbSelectServer;        
        private System.Windows.Forms.Label lblSelectRack;
        private System.Windows.Forms.Label lblSelectServer;
        #endregion       
        
        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        public FrmSelectWorksheet()
        {
            InitializeComponent();
            this.lblSelectColo.Text = Global.GetResourceString("FormWorksheetSelect1_Label");
            this.lblSelectRack.Text = Global.GetResourceString("FormWorksheetSelect2_Label");
            this.lblSelectServer.Text = Global.GetResourceString("FormWorksheetSelect3_Label");  
            this.titleLabel.Text = Global.GetResourceString("FormWorksheetTitle_Text");

            this.HeaderPanel.Controls.Add(this.titleLabel);
            this.HeaderPanel.ResumeLayout(false);
        }
        #endregion Constructor

        #region Wizard Methods
        /// <summary>
        /// On Form Active
        /// </summary>
        /// <returns></returns>
        protected override bool OnSetActive()
        {
            if (!base.OnSetActive())
                return false;

            // Enable both the Next and Back buttons on this page    
            Wizard.SetWizardButtons(WizardButtons.Back | WizardButtons.Next);

            try
            {
                // Clear sheet name comboboxes
                this.cmbSelectColo.Items.Clear();
                this.cmbSelectRack.Items.Clear();
                this.cmbSelectServer.Items.Clear();

                // Loop through each excel sheet name and add into comboboxes
                foreach (string excelsheet in Global.ExcelSheetsName)
                {
                    this.cmbSelectColo.Items.Add(excelsheet);
                    this.cmbSelectRack.Items.Add(excelsheet);
                    this.cmbSelectServer.Items.Add(excelsheet);
                }

                this.cmbSelectColo.SelectedIndex = Global.CmbColoIndex;
                this.cmbSelectRack.SelectedIndex = Global.CmbRackIndex;
                this.cmbSelectServer.SelectedIndex = Global.CmbServerIndex;
                   
                
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);

            }
            return true;
        }

        /// <summary>
        /// On Back clicked
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardBack()
        {                      
            // Form Import
            return "FrmImport";                      
        }

        /// <summary>
        /// On Next clicked
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardNext()
        {
            try
            {         
                // Get selected server, rack, colo sheet names
                Global.SheetServerSelect = cmbSelectServer.SelectedItem.ToString();
                Global.SheetRackSelect = cmbSelectRack.SelectedItem.ToString();
                Global.SheetColoSelect = cmbSelectColo.SelectedItem.ToString();

                // Sql Command to retrieve data from excel
                string sqlServerDetails = @"SELECT * FROM [" + Global.SheetServerSelect + "]";
                string sqlRackDetails = @"SELECT * FROM [" + Global.SheetRackSelect + "]";
                string sqlColoDetails = @"SELECT * FROM [" + Global.SheetColoSelect + "]";

                // Get column names of respective excel sheets
                Global.ExcelServerColumnNames = ExcelServiceManager.GetColumnNamesFromExcel(sqlServerDetails);
                Global.ExcelRackColumnNames = ExcelServiceManager.GetColumnNamesFromExcel(sqlRackDetails);
                Global.ExcelColoColumnNames = ExcelServiceManager.GetColumnNamesFromExcel(sqlColoDetails);

                // Set colo, rack, server comboboxes index
                Global.CmbColoIndex = this.cmbSelectColo.SelectedIndex;
                Global.CmbRackIndex = this.cmbSelectRack.SelectedIndex;
                Global.CmbServerIndex = this.cmbSelectServer.SelectedIndex;

                // Form Select Columns
                return "FrmSelectColumns";
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

                return null;
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);

                return null;
            }
        }
        #endregion Wizard Methods

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cmbSelectColo = new System.Windows.Forms.ComboBox();
            this.lblSelectColo = new System.Windows.Forms.Label();
            this.titleLabel = new System.Windows.Forms.Label();
            this.cmbSelectRack = new System.Windows.Forms.ComboBox();
            this.cmbSelectServer = new System.Windows.Forms.ComboBox();
            this.lblSelectRack = new System.Windows.Forms.Label();
            this.lblSelectServer = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIcon)).BeginInit();
            this.SuspendLayout();
            // 
            // cmbSelectColo
            // 
            this.cmbSelectColo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSelectColo.FormattingEnabled = true;
            this.cmbSelectColo.Location = new System.Drawing.Point(15, 103);
            this.cmbSelectColo.Name = "cmbSelectColo";
            this.cmbSelectColo.Size = new System.Drawing.Size(471, 21);
            this.cmbSelectColo.TabIndex = 0;
            // 
            // lblSelectColo
            // 
            this.lblSelectColo.AutoSize = true;
            this.lblSelectColo.Location = new System.Drawing.Point(15, 83);
            this.lblSelectColo.Name = "lblSelectColo";
            this.lblSelectColo.Size = new System.Drawing.Size(0, 13);
            this.lblSelectColo.TabIndex = 0;
            // 
            // titleLabel
            // 
            this.titleLabel.BackColor = System.Drawing.Color.White;
            this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.titleLabel.Location = new System.Drawing.Point(13, 11);
            this.titleLabel.Name = "titleLabel";
            this.titleLabel.Size = new System.Drawing.Size(415, 13);
            this.titleLabel.TabIndex = 3;
            // 
            // cmbSelectRack
            // 
            this.cmbSelectRack.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSelectRack.FormattingEnabled = true;
            this.cmbSelectRack.Location = new System.Drawing.Point(15, 159);
            this.cmbSelectRack.Name = "cmbSelectRack";
            this.cmbSelectRack.Size = new System.Drawing.Size(471, 21);
            this.cmbSelectRack.TabIndex = 3;
            // 
            // cmbSelectServer
            // 
            this.cmbSelectServer.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSelectServer.FormattingEnabled = true;
            this.cmbSelectServer.Location = new System.Drawing.Point(15, 215);
            this.cmbSelectServer.Name = "cmbSelectServer";
            this.cmbSelectServer.Size = new System.Drawing.Size(471, 21);
            this.cmbSelectServer.TabIndex = 5;
            // 
            // lblSelectRack
            // 
            this.lblSelectRack.AutoSize = true;
            this.lblSelectRack.Location = new System.Drawing.Point(15, 140);
            this.lblSelectRack.Name = "lblSelectRack";
            this.lblSelectRack.Size = new System.Drawing.Size(0, 13);
            this.lblSelectRack.TabIndex = 2;
            // 
            // lblSelectServer
            // 
            this.lblSelectServer.AutoSize = true;
            this.lblSelectServer.Location = new System.Drawing.Point(15, 195);
            this.lblSelectServer.Name = "lblSelectServer";
            this.lblSelectServer.Size = new System.Drawing.Size(0, 13);
            this.lblSelectServer.TabIndex = 4;
            // 
            // FrmSelectWorksheet
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lblSelectServer);
            this.Controls.Add(this.lblSelectRack);
            this.Controls.Add(this.cmbSelectServer);
            this.Controls.Add(this.cmbSelectRack);
            this.Controls.Add(this.lblSelectColo);
            this.Controls.Add(this.cmbSelectColo);
            this.Name = "FrmSelectWorksheet";
            this.Size = new System.Drawing.Size(502, 282);
            this.Load += new System.EventHandler(this.FrmSelectWorksheet_Load);
            this.Controls.SetChildIndex(this.cmbSelectColo, 0);
            this.Controls.SetChildIndex(this.lblSelectColo, 0);
            this.Controls.SetChildIndex(this.cmbSelectRack, 0);
            this.Controls.SetChildIndex(this.cmbSelectServer, 0);
            this.Controls.SetChildIndex(this.lblSelectRack, 0);
            this.Controls.SetChildIndex(this.lblSelectServer, 0);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIcon)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion       
     
        #region Events
        /// <summary>
        /// On Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSelectWorksheet_Load(object sender, EventArgs e)
        {
            try
            {
                // Header panel office icon
                System.IO.Stream xsdResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RackServerManager.Images.office.ico");
                base.pictureBoxIcon.Image = System.Drawing.Image.FromStream(xsdResourceStream);               
            }
            catch (RackException ex)
            {

                // To catch and display exceptions from sub functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
            }
            catch (Exception ex)
            {
                // To catch exceptions from current functions
                RackException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);
            }
        }
        #endregion Events
    }
}
